﻿
#region Copyright & License
// --------------------------
// Copyright 2008 Daniel Gray
// --------------------------
// This file is part of Kale.
//
// Kale is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Kale is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Kale.  If not, see <http://www.gnu.org/licenses/>.
#endregion
#region File details
// --------------------------------------------------------
// Filename          : Image.ashx.cs
// Author            : Daniel Gray
// Created           : 20/04/2008
// --------------------------------------------------------
#endregion
            
using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Drawing;
using Kale.Core;
using Kale.Web.Services;
using System.Drawing.Imaging;
using Kale.Core.Configuration;
using log4net;
using System.IO;

namespace Kale.Web.GUI
{
   /// <summary>
   /// Summary description for $codebehindclassname$
   /// </summary>
   [WebService(Namespace = "http://tempuri.org/")]
   [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
   public class Image : IHttpHandler
   {
      private static ILog _logger = LogManager.GetLogger(typeof(Image));
      private static bool _hasConfigured = false;

      private static void Configure(HttpContext context)
      {
         if (!_hasConfigured)
         {
            _hasConfigured = true;
         }
      }

      public void ProcessRequest(HttpContext context)
      {
         //TODO:Correct the image format
         Configure(context);

         string sId = context.Request.QueryString["id"];
         string sNewSize = context.Request.QueryString["newSize"];

         if (string.IsNullOrEmpty(sId)) return;
         if (string.IsNullOrEmpty(sNewSize)) return;

         Size newSize = ImageSize.GetSizeFromName(sNewSize);
         IImageNode node = RootNodeProvider.Instance.LoadNode(NodeId.ParseFromIdString(sId));
         string key = node.NodeId.GetCacheKey(newSize);
         byte[] outputData;

         if (!ImageCache.HasImageInCache(node, newSize, ImageFormat.Jpeg))
         {
            byte[] resizedImage = ImageResizer.ResizeImage(node.OpenImage(), ImageFormat.Jpeg, newSize);
            ImageCache.AddImageToCache(node, newSize, ImageFormat.Jpeg, resizedImage);
            outputData = resizedImage;
         }
         else
         {
            outputData = ImageCache.GetImageFromCache(node, newSize, ImageFormat.Jpeg);
         }

         context.Response.ContentType = "image/jpeg";
         context.Response.OutputStream.Write(outputData, 0, outputData.Length);
      }

      public bool IsReusable
      {
         get
         {
            return true;
         }
      }
   }
}
